package com.cn.steam.iam.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.steam.foundation.common.model.exception.BadRequestException;
import com.cn.steam.foundation.common.utils.Op;
import com.cn.steam.iam.mapper.iam.ExtClientUserMapper;
import com.cn.steam.iam.mapper.iam.IamUserMapper;
import com.cn.steam.iam.model.po.ExtClientUser;
import com.cn.steam.iam.model.po.IamUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.cn.steam.foundation.common.model.CommonMessage.*;
import static com.cn.steam.iam.model.IamUserConstant.CLIENT_USER_SSL_TYPE;
import static com.cn.steam.iam.model.IamUserConstant.USER_TYPE_OAUTH_CLIENT;

/**
 * @author huangzz
 */
@Service
public class ExtClientUserProfileService extends ServiceImpl<ExtClientUserMapper, ExtClientUser> {
	@Autowired
	private IamUserMapper iamUserMapper;
	public void addClientUserMappingForSsl(String partnerId, String sslUserId)  {
		IamUser user = iamUserMapper.selectByPartnerIdAndType(partnerId, USER_TYPE_OAUTH_CLIENT);
		if(user == null){
			throw new BadRequestException(Op.str(INVALID_PARAMETER_VALUE, "partnerId", partnerId));
		}
		ExtClientUser extClientUser = new ExtClientUser();
		extClientUser.setClientId(user.getLoginName());
		extClientUser.setSslType(CLIENT_USER_SSL_TYPE);
		extClientUser.setSslUserId(sslUserId);
		getBaseMapper().insert(extClientUser);
	}

	public ExtClientUser selectBySslUserIdAndSslType(String sslType, String sslUserId){
		return getBaseMapper().selectBySslUserIdAndSslType(sslType, sslUserId);
	}
}
